Repository navigation
Conversation
jsep parses `?.`, `?.[]` and `?.()` and marks the node `optional`, but the safe evaluator ignored the flag. So under the default eval: "safe" a filter such as $.books[?(@.meta?.tag === "x")] threw "Cannot read properties of undefined (reading 'tag')" as soon as one item had no `meta`, while eval: "native" returned the match. A member or call link now returns a short-circuit marker when it is optional and its object or callee is null or undefined; the outer links pass it up and the whole chain evaluates to undefined, like in JS. The object is now evaluated before the property and the callee before the arguments (JS order), so a skipped `?.[expr]` or `?.(args)` does not run `expr` or `args`.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR description
Fixes #295.
jsep already parses
?.,?.[...]and?.()and setsoptional: trueon the node, but the safe evaluator ignored it. So with the defaulteval: "safe",$.books[?(@.meta?.tag === "x")]threwCannot read properties of undefined (reading 'tag')on the first item withoutmeta, whileeval: "native"worked.What changed in
src/Safe-Script.js:nullorundefinedreturns a short-circuit marker. Outer links pass it up, andevalAstturns it intoundefinedat the end of the chain. So@.a?.b.c()givesundefinedwhen@.ais missing, like in JS.?.[expr]or?.(args)does not runexpr/args.(a?.b).cparses the same asa?.b.cand is also short-circuited. I think that is the better side to fall on, since the unparenthesized form is the common one.The security checks (
getSafeProperty, blocked functions) run on the same values as before.I also compared the safe evaluator against plain JS on 41 member/call chain expressions (nullish and non-nullish links, computed keys, optional calls, side effects in skipped parts). Main differs on 23 of them, this branch on none.
Checklist
npm test, ensuring linting passes